/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.gui.filtersmanager.nodesmanaging;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreePath;

/**
 *
 ** @author Federico Improta
 */
public class LeafEditor extends AbstractCellEditor implements TreeCellEditor, ValueChangedEventListener {

	LeafRenderer renderer = new LeafRenderer();
	Component editor;
	ChangeEvent changeEvent = null;
	JTree tree;

	public LeafEditor(JTree tree) {
		this.tree = tree;
	}

	@Override
	public Object getCellEditorValue() {
		if (editor instanceof IVisualLeafEditor) {
			return ((IVisualLeafEditor) editor).getValue();
		} else {
			return null;
		}
	}

	@Override
	public boolean isCellEditable(EventObject event) {
		boolean returnValue = false;
		if (event instanceof MouseEvent) {
			MouseEvent mouseEvent = (MouseEvent) event;
			TreePath path = tree.getPathForLocation(mouseEvent.getX(),
					  mouseEvent.getY());
			if (path != null) {
				Object node = path.getLastPathComponent();
				if ((node != null) && (node instanceof IEditableTreeLeaf)) {
					returnValue = true;
				}
			}
		}
		return returnValue;
	}

	@Override
	public Component getTreeCellEditorComponent(JTree tree, final Object value,
			  boolean selected, boolean expanded, boolean leaf, int row) {

		editor = renderer.getTreeCellRendererComponent(tree, value,
				  true, expanded, leaf, row, true);

		// editor always selected / focused

		if ((editor instanceof IVisualLeafEditor)&&(value instanceof IEditableTreeLeaf)) {
			((IVisualLeafEditor) editor).setLeaf((IEditableTreeLeaf)value);
			((IVisualLeafEditor) editor).addListener(this);
		}


		return editor;
	}



	@Override
	public void valueChanged(IEditableTreeLeaf leaf) {
		if ((leaf != null) && (leaf instanceof IEditableTreeLeaf)) {
			IEditableTreeLeaf tl = (IEditableTreeLeaf) leaf;
			tl.setValue(getCellEditorValue());
		}
		if (stopCellEditing()) {
			fireEditingStopped();
		}
	}//public void valueChanged(IEditableTreeLeaf leaf)
}
